package domain;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import dal.ConnectionManager;

public class Task {
	/** id of the task */  
	private Integer id;
	/** name of task */
	private String name;
	/** description of task */
	private String desc;
	/** starting date of project */
	private Date startDate = null;
	/** hours of task */
	private Integer hours;
	/** resourceid of task */
	private Integer resourceId;
	/** deliverable of task */
	private String deliverable;
	/** activityId of activity which task connects */
	private Integer activityId;
	/** activity which task connects */
	private Activity myActivity;
	/** This is used to converting the date yyyy-MM-dd format */
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	/** These instances are needed for database connection */
	ConnectionManager mgr = ConnectionManager.getInstance();
	Connection con = mgr.getConnection();
	
	/** Empty constructor of the Task class */
	public Task() {
		super();
	}
	/**
	 * Constructor of the Task class
	 *                           
	 * @param name Name of the task
	 * @param desc Description of the task
	 * @param dateInput Starting date of the task
	 * @param hours Hours of the task
	 * @param deliverable Deliverable of the task
	 * @param activityId activity id which task connects
	 * @param resourceId resource id of the task
	 */
	public Task(String name, String desc, String dateInput, Integer hours, String deliverable,
			Integer activityId, Integer resourceId) {
		super();
		this.name = name;
		this.desc = desc;
		setStartDate(dateInput);
		this.hours = hours;
		this.deliverable = deliverable;
		this.activityId = activityId;
		this.resourceId = resourceId;
	}
	/**
	 * The method for creating a task
	 *                            
	 * @param activity Object of the Activity
	 * @param myTask Object of the Task
	 * @return void 
	 */
	public boolean create(Activity myActivity, Task myTask)
	{
		if(myTask.getStartDate().before(myActivity.getStartDate()))
		{
			return false;
		}
		else{
		myTask.setActivityId(myActivity.getId());
		myTask.addTask(myTask);

		return true;
		}
	}
	
	/**
	 * Adding a task to database
	 * @param myTask
	 */
	public void addTask(Task myTask)
    {
		try {
			Statement st = con.createStatement();
			st.executeUpdate("insert into task (name, description, startDate, hours, deliverable, activityId, resourceId) values ('"+myTask.getName()+"', '"+myTask.getDesc()+"', '"+myTask.getDateToString()+"', "+myTask.getHours()+", '"+myTask.getDeliverable() +"', "+myTask.getActivityId()+", "+myTask.getResourceId()+")");
			ResultSet rs = st.executeQuery("select id from task where name='"+ myTask.getName() +"' and description='"+ myTask.getDesc() +"' and startDate='"+ myTask.getDateToString()+"'");
			while (rs.next()) {
				myTask.setId(rs.getInt(1));	
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	
	/**
	 * Removing a task from database
	 * @param myTask
	 */
	public void removeTask(Task myTask)
    {
		try {
			Statement st = con.createStatement();
			st.executeUpdate("delete from task where id="+ myTask.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	
	/**
	 * Updating a task
	 * @param myTask
	 */
	public void updateTask(Task myTask)
	{
	    try {
			Statement st = con.createStatement();
			st.executeUpdate("update task set name='"+ myTask.getName() +"', description='"+ myTask.getDesc() +"', startDate='"+ myTask.getDateToString() +"', hours="+ myTask.getHours() +", deliverable='"+ myTask.getDeliverable() +"', activityId="+ myTask.getActivityId() +", resourceId="+ myTask.getResourceId() +" where id="+myTask.getId());
	    } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Finding the task that given its id
	 * @param id
	 * @return Task 
	 */
	public Task selectById(Integer id) 
	{
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from task where id="+id);
			Task myTask = new Task();
			if (rs.next()) {
			     do {
						myTask.setId(rs.getInt(1));
						myTask.setName(rs.getString(2));
						myTask.setDesc(rs.getString(3));
						myTask.setStartDate(rs.getString(4));
						myTask.setHours(rs.getInt(5));
						myTask.setDeliverable(rs.getString(6));
						myTask.setActivityId(rs.getInt(7));
						myTask.setResourceId(rs.getInt(8));
					
			       } while (rs.next());
			 	return myTask;  
			     }
			  else {
			    return null;
		}			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Finding the tasks that connect the given activity
	 * @param myActivity
	 * @return ArrayList<Task>
	 */
	public ArrayList <Task> selectbyActivity(Activity myActivity) 
	{
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from task where activityId="+myActivity.getId());
			ArrayList <Task> myTaskList = new ArrayList <Task>();	
			if (rs.next()) {
			     do {
			    	 	Task myTask = new Task();
						myTask.setId(rs.getInt(1));
						myTask.setName(rs.getString(2));
						myTask.setDesc(rs.getString(3));
						myTask.setStartDate(rs.getString(4));
						myTask.setHours(rs.getInt(5));
						myTask.setDeliverable(rs.getString(6));
						myTask.setActivityId(rs.getInt(7));
						myTask.setResourceId(rs.getInt(8));
						myTaskList.add(myTask);
			       } while (rs.next());
			 	return myTaskList;  
			     }
			  else {
			    return null;
		}			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Finding the tasks which connect the given activity
	 * @param myActivity
	 * @return ResultSet
	 */
	public ResultSet selectTaskbyActivity(Activity myActivity) 
	{
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from task where activityId="+myActivity.getId());
			 return rs;
		}	
		 catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Printing the tasks which connect the given activity on console
	 * @param myActivity
	 */
	public void writeSelectByActivity(Activity myActivity) 
	{
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from task where activityId="+myActivity.getId());
			while(rs.next())
			{
			   System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4)+" "+rs.getInt(5)+" "+rs.getString(6)+" "+rs.getInt(7)+" "+rs.getInt(8));	
			}
						
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
	}
	
	/**
	 * Getters and setters
	 * @return
	 */
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public Date getStartDate() {
		return startDate;
	}
	
	public void setStartDate(String dateInput) {

		Activity myAct = new Activity();
		
		if(myAct.selectById(activityId)!=null)
		{
			if(startDate.before(myAct.selectById(activityId).getStartDate()))
			{
				System.out.println("THE TASK STARTDATE CAN NOT BE BEFORE THE ACTIVITY STARTDATE!!!");
			}
			else
			{
				try {  
					   startDate = format.parse(dateInput);
				    } catch (ParseException e) {
				    	e.printStackTrace();
				    }
			}
		}
		
		else{
			if(dateInput==null)
	    	{
	    	   startDate = null;	
	    	}
			else{
				
				try {  
					   startDate = format.parse(dateInput);
				    } catch (ParseException e) {
				    	e.printStackTrace();
				    }
			}
			
		}

	}
	
	public String getDateToString()
	{
		return (format.format(startDate));
	}
	public Integer getHours() {
		return hours;
	}

	public void setHours(Integer hours) {
		this.hours = hours;
	}

	public String getDeliverable() {
		return deliverable;
	}

	public void setDeliverable(String deliverable) {
		this.deliverable = deliverable;
	}

	public Integer getResourceId() {
		return resourceId;
	}

	public void setResourceId(Integer resourceId) {
		this.resourceId = resourceId;
	}

	public Integer getActivityId() {
		return activityId;
	}

	public void setActivityId(Integer activityId) {
		this.activityId = activityId;
	}
	public Activity getMyActivity() {
		return myActivity;
	}
	public void setMyActivity(Activity myActivity) {
		this.myActivity = myActivity;
	}
}

